package org.jeecg.modules.cw.service.impl;

import org.jeecg.modules.cw.entity.CwJbhyhpxjhbTj;
import org.jeecg.modules.cw.entity.CwJbhyhpxjhb;
import org.jeecg.modules.cw.mapper.CwJbhyhpxjhbMapper;
import org.jeecg.modules.cw.mapper.CwJbhyhpxjhbTjMapper;
import org.jeecg.modules.cw.service.ICwJbhyhpxjhbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 举办会议和培训计划提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-10
 * @Version: V1.0
 */
@Service
public class CwJbhyhpxjhbTjServiceImpl extends ServiceImpl<CwJbhyhpxjhbTjMapper, CwJbhyhpxjhbTj> implements ICwJbhyhpxjhbTjService {

	@Autowired
	private CwJbhyhpxjhbTjMapper cwJbhyhpxjhbTjMapper;
	@Autowired
	private CwJbhyhpxjhbMapper cwJbhyhpxjhbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwJbhyhpxjhbTj cwJbhyhpxjhbTj, List<CwJbhyhpxjhb> cwJbhyhpxjhbList) {
		cwJbhyhpxjhbTjMapper.insert(cwJbhyhpxjhbTj);
		if(cwJbhyhpxjhbList!=null && cwJbhyhpxjhbList.size()>0) {
			for(CwJbhyhpxjhb entity:cwJbhyhpxjhbList) {
				//外键设置
				entity.setMainId(cwJbhyhpxjhbTj.getId());
				cwJbhyhpxjhbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwJbhyhpxjhbTj cwJbhyhpxjhbTj,List<CwJbhyhpxjhb> cwJbhyhpxjhbList) {
		cwJbhyhpxjhbTjMapper.updateById(cwJbhyhpxjhbTj);
		
		//1.先删除子表数据
		cwJbhyhpxjhbMapper.deleteByMainId(cwJbhyhpxjhbTj.getId());
		
		//2.子表数据重新插入
		if(cwJbhyhpxjhbList!=null && cwJbhyhpxjhbList.size()>0) {
			for(CwJbhyhpxjhb entity:cwJbhyhpxjhbList) {
				//外键设置
				entity.setMainId(cwJbhyhpxjhbTj.getId());
				cwJbhyhpxjhbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwJbhyhpxjhbMapper.deleteByMainId(id);
		cwJbhyhpxjhbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwJbhyhpxjhbMapper.deleteByMainId(id.toString());
			cwJbhyhpxjhbTjMapper.deleteById(id);
		}
	}
	
}
